﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class Skill : MonoBehaviour
{
    public Player player;
    public Player enemy;
    public float damage;
    public float SkillCD = 5;
    public Image slider;
    public ZhenYingType type;

    public enum ZhenYingType
    {
        Player,
        Enemy,
    }

    private float Cd = float.MaxValue;

    public bool Runner()
    {
        Cd -= Time.deltaTime;
        slider.fillAmount = Cd / SkillCD;

        return Cd <= 0;
    }

    public void ResetSkill()
    {
        Cd = SkillCD;
        slider.fillAmount = Cd / SkillCD;
    }

    public IEnumerator Play()
    {
        yield return PlayEnterAnim();
        yield return DoDamage();
        yield return PlayExitAnim();
    }

    private Vector3 origin;
    private IEnumerator PlayEnterAnim()
    {
        origin = player.transform.position;
        player.PlayAnim("Move");
        player.transform.DOMove(enemy.transform.position, 0.5f);
        yield return new WaitForSeconds(0.5f);
        yield break;
    }

    private IEnumerator PlayExitAnim()
    {
        player.body.transform.localEulerAngles = new Vector3(0f, (player.body.transform.localEulerAngles.y + 180f) % 360, 0f);
        player.PlayAnim("Move");
        player.transform.DOMove(origin, 0.5f);
        yield return new WaitForSeconds(0.5f);
        player.body.transform.localEulerAngles = new Vector3(0f, (player.body.transform.localEulerAngles.y + 180f) % 360, 0f);
        player.PlayAnim("Idle");
        yield return new WaitForSeconds(0.2f);
    }

    private IEnumerator DoDamage()
    {
        player.PlayAnim("Attack");
        yield return new WaitForSeconds(1f);
        enemy.TakeDamage(10);
        yield return new WaitForSeconds(0.3f);
    }

}
